package stu.aistar.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import stu.aistar.model.pojo.Ciname;
import stu.aistar.model.pojo.Movie;
import stu.aistar.model.vo.Result;
import stu.aistar.service.CinameService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cinames")
public class CinameController {

    @Autowired
    private CinameService cinameService;

    @RequestMapping("/findAll")
    public Map<String,Object> findAll(HttpServletRequest request){
        String page = request.getParameter("page");
        String pageSize = request.getParameter("limit");
        String params = request.getParameter("searchParams");
        String name=null;
        //先转换成JSONObject类型
        if (null != params){
            JSONObject obj = JSONObject.parseObject(params);
            name= obj.getString("name");
        }
        Map<String,Object> map = new HashMap<>();
        PageInfo<Ciname> pageInfo = cinameService.findByPage(page, pageSize,name);
        map.put("code",0);
        map.put("msg","查找成功");
        //数据总数
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }


    @PostMapping("/save")
    public Result saveCiname(Ciname ciname){
        Result result;
        Integer row = cinameService.saveMovie(ciname);
        if(row == 1){
            result = new Result("200","添加成功",null);
        }else {
            result = new Result("500","添加失败",null);
        }
        return result;
    }

    @PostMapping("/deleteById")
    public Result deleteCiname(@RequestParam("id") Integer id){
        Result result;
        Integer row = cinameService.deleteCinameById(id);
        if(row == 1){
            result = new Result("200","删除成功",null);
        }else {
            result = new Result("500","删除失败",null);
        }
        return result;
    }


    @PostMapping("/editCiname")
    public Result editCiname(Ciname ciname){
        Result result;
        Integer row = cinameService.editCiname(ciname);
        if(row== 1){
            result = new Result("200","修改成功",null);
        }else {
            result = new Result("500","查找失败",null);
        }
        return result;
    }
}
